﻿

//    <script src="/JS/jQuery/jquery.sp.js" type="text/javascript"></script>
//    <script src="/JS/jQuery/jquery.ajaxupload.js" type="text/javascript"></script>
//    <script type="text/javascript" src="/JS/upLoad.js"></script>

//$(function () {uploadLogoImg("selectImage","uploading","180","100","photologo","txt_productLogo","photohref","logobox");});
//正则扩展名,监控按钮ID,信息提示ID,小图宽,小图高,显示图片ID,值字段ID,链接ID,框ID
var uploadLogoImg = function (btn_ID, info_id, thumbW, thumbH, img_id, val_id, href_id, box_id) {
    var allowFileExt = /^(png|jpg)$/;
    var interval;
    new AjaxUpload(btn_ID, {
        action: '/UploadFile.ashx',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, ext) {
            if (ext && allowFileExt.test(ext.toString().toLowerCase())) {
                this.setData({
                    'uploadType': '/File',
                    'twidth': thumbW,
                    'theight': thumbH,
                    'baseRoot': baseRoot,
                    'rootPath': rootPath
                });
            } else {
                alert("您选择的不是正确的JPG图片！");
                return false;
            }
            $("#" + info_id).text("Uploading");
            // Uploding -> Uploading. -> Uploading...
            interval = window.setInterval(function () {
                var text = $("#" + info_id).text();
                if (text.length < 13) {
                    $("#" + info_id).text(text + '.');
                } else {
                    $("#" + info_id).text('Uploading');
                }
            }, 200);
        },
        onComplete: function (file, json) {
            window.clearInterval(interval);
            if (json && json.files && json.files.length > 0) {
                $("#" + info_id).text("Uploaded " + json.files.length + " file.");
                if (!json.files[0].isUpload) {
                    alert("上传失败");
                    $("#" + info_id).text('上传失败');
                }
                else {
                    var fp = json.files[0].filePath;
                    $("#" + val_id).val(fp);
                    var tfp = GetFile(getSImage(fp));
					fp=GetFile(fp);
					
                    if ($("#" + img_id).length > 0) {
                        $("#" + img_id).attr("src", tfp);
                        $("#" + href_id).attr("href", fp);
                    }
                    else {
                        var formatstr = "<a href=\"{0}\" target=\"_blank\" id=\"" + href_id + "\" title=\"点击查看\"><img id=\"" + img_id + "\"  src=\"{1}\" height=\"50\" /></a>";
                        $("#" + box_id).append($.sp.format(formatstr, fp, tfp));
                    }
                    $("#" + info_id).text('上传成功');
                }
            }
            else {
                var errStr = "";
                if (json) {
                    if (json.info) errStr = json.info;
                    else if (json.resultStr) errStr = json.resultStr;
                }
                $("#" + info_id).text('上传失败 ' + errStr);
            }
        }
    });
}

var getSImage = function (filePath) {
    var fileName = filePath.toString().substring(filePath.toString().lastIndexOf("/"), filePath.toString().length);
    return (filePath.toString().replace(fileName, "/thumb" + fileName));
}

var GetFile = function (filePath) {
    return (filePath.toString().replace("../..", baseRoot));
}